<template>
    <el-card>
        <el-card>
            <!--<el-row class="form-row">
                <el-form :model="filters" label-position="right" inline ref="formInline">
                    <el-form-item label="父级菜单">
                        <div class="filters-input">
                            <treeselect :options="treeList" noChildrenText="没有子节点" noOptionsText="暂无选项"
                                        noResultsText="没有发现结果" placeholder="选择父级菜单" :show-count="true"
                                        v-model="filters.parentId"/>
                        </div>
                    </el-form-item>
                    <el-form-item label="菜单名称">
                        <div class="filters-input">
                            <el-input v-model="filters.name" placeholder="请输入菜单名称"></el-input>
                        </div>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="ios-search" @click="handleQuery('formInline')">搜索</el-button>
                    </el-form-item>
                </el-form>
            </el-row>-->
            <el-row class="form-row">
                <el-button type="primary" @click="addMenu()">新增</el-button>
            </el-row>
            <el-table stripe border :loading="tableLoading" :data="tableData" @expand-change="menuInfo" ref="table"
                      style="width: 100%">
                <template slot="empty">暂无数据</template>
                <el-table-column type="expand" v-loading="menuTableLoading">
                    <template slot-scope="props">
                            <el-row :gutter="20">
                                <el-col :span="8">菜单名称</el-col>
                                <el-col :span="8">权限唯一键</el-col>
                                <el-col :span="8">操作</el-col>
                            </el-row>
                            <div style="border: 1px solid #E0E0E0;margin:5px 0px"></div>
                            <div class="bill-item" v-for="(item,index) in subMenu" :key="index">
                                <el-row :gutter="20">
                                    <el-col :span="8">{{item.name}}</el-col>
                                    <el-col :span="8">{{item.authority}}</el-col>
                                    <el-col :span="8">
                                        <el-button size="mini" type="primary" @click="addMenu(item)">修改</el-button>
                                        <el-button size="mini" type="danger" @click="handleDelete(item.id)">删除</el-button>
                                    </el-col>
                                </el-row>
                                <div style="border: 1px solid #E0E0E0;margin:5px 0px"></div>
                            </div>
                    </template>
                </el-table-column>
                <el-table-column label="菜单名称" prop="name"></el-table-column>
                <el-table-column label="权限唯一键" prop="authority"></el-table-column>
                <el-table-column label="操作" width="150" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini" type="primary" @click="addMenu(scope.row)">修改
                        </el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <Page style="margin: 10px 0;" :total="pageTotal" :current="pageNum" :page-size="pageSize" show-total
                  id="lyPage"
                  show-sizer show-elevator @on-change="handlePage" @on-page-size-change='handlePageSize'>
            </Page>
        </el-card>

        <el-dialog :close-on-click-modal="false" title="菜单管理" :visible.sync="menuConfig" width="900">
            <div class="pp-info">
                <el-form :model="menu" :rules="rules" ref="menuForm" label-width="100px"
                         class="demo-ruleForm">
                    <el-form-item label="父级菜单">
                        <treeselect :options="treeList" noChildrenText="没有子节点" noOptionsText="暂无选项"
                                    noResultsText="没有发现结果" placeholder="选择父级菜单" :show-count="true"
                                    v-model="menu.parentId"/>
                    </el-form-item>
                    <el-form-item label="菜单名称" prop="name">
                        <el-input v-model="menu.name" placeholder="菜单名称"></el-input>
                    </el-form-item>
                    <el-form-item label="权限唯一键" prop="authority">
                        <el-input v-model="menu.authority" placeholder="权限唯一键"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="menuConfig = false">取 消</el-button>
                <el-button type="primary" @click="suremenu('menuForm')">确 定</el-button>
            </div>
        </el-dialog>
    </el-card>
</template>


<script>
    import Treeselect from "@riophae/vue-treeselect";

    export default {
        components: {
            Treeselect,
        },
        data() {
            return {
                //校验规则
                rules: {
                    name: [
                        {
                            required: true,
                            message: "请填写菜单名称",
                        }
                    ],
                    authority: [
                        {
                            required: true,
                            message: "请填写权限唯一键",
                        }
                    ],
                },
                //查询条件
                filters: {},
                treeList: [],
                //页码
                pageNum: 1,
                //每页数量
                pageSize: 10,
                //总数
                pageTotal: 0,
                tableData: [],
                //表单提交状态
                tableLoading: false,
                menuConfig: false,
                menu: {},
                value: null,
                menuTableLoading: false,
                subMenu: [],
            }
        },
        created: function () {
            this.getMenuTreeList();
            this.loadData();
        },
        methods: {
            //加载数据
            loadData: function () {
                if (this.tableLoading) return;
                this.tableLoading = true;
                let params = this.filters;
                params.pageNum = this.pageNum;
                params.pageSize = this.pageSize;
                //与后台交换数据
                this.postRequest("/api/sysMenu/loadData", params).then(res => {
                    console.log(res)
                    this.tableLoading = false;
                    if (res.code == 20000) {
                        this.tableData = res.data.list;
                        this.pageTotal = res.data.total;
                    } else {
                        this.tableData = [];
                        this.$message.error(res.message);
                    }
                });
            },
            //选择父菜单，加载子菜单
            menuInfo(row, expandedRows) {
                if (expandedRows.length > 1) {
                    expandedRows.shift();
                }
                this.getRequest("/api/sysMenu/getSubMenuList?parentId=" + row.id).then(res => {
                    console.log(res)
                    if (res.code == 20000) {
                        this.subMenu = res.data;
                    } else {
                        this.$message.error(res.message);
                    }
                });
            },
            //加载菜单树
            getMenuTreeList() {
                this.getRequest("/api/sysMenu/getMenuTreeList").then(res => {
                    console.log(res);
                    if (res.code == 20000) {
                        this.treeList = res.data;
                    } else {
                        this.$message.error(res.message);
                    }
                });
            },
            //打开新增修改页面
            addMenu(row) {
                console.log(row);
                this.menu = {};
                if (row) {
                    this.menu = row;
                }
                this.menuConfig = true;
            },
            //保存新增修改
            suremenu: function (menuForm) {
                this.$refs[menuForm].validate((valid) => {
                    if (valid) {
                        console.log(this.menu);
                        this.postRequest("/api/sysMenu/saveOrUpdate", this.menu).then(res => {
                            if (res.code == 20000) {
                                this.menuConfig = false;
                                this.$Message.success({
                                    content: "操作成功",
                                    duration: 5
                                });
                                this.loadData();
                                this.getMenuTreeList();
                            } else {
                                this.$message.error(res.message);
                            }
                        });
                    }
                });
            },
            //删除
            handleDelete(id) {
                this.$Modal.confirm({
                    title: "提示",
                    content: "确定删除该条记录？",
                    onOk: () => {
                        this.getRequest("/api/sysMenu/deleteMenu?id=" + id).then(res => {
                            if (res.code == 20000) {
                                this.$Message.success({
                                    content: "操作成功",
                                    duration: 5
                                });
                                this.loadData();
                            } else {
                                this.$message.error(res.message);
                            }
                        });
                    }
                });
            },
            //查询
            handleQuery() {
                this.loadData();
            },
            handlePage(value) {
                this.pageNum = value;
                this.loadData();
            },
            handlePageSize(value) {
                this.pageSize = value;
                this.loadData();
            },
        }
    };
</script>
<style lang="less" scoped>
    #pop {
        width: 780px;
        height: 600px;
        display: none;
        position: fixed;
        z-index: 10;
        top: 10%;
        left: 50%;
        margin-left: -390px;
    }
</style>
